/*******************************************************************************
 * Copyright (c) 2012, G. Weirich and Elexis
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    G. Weirich - initial implementation
 *******************************************************************************/

package ch.elexis.easy.ui.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

import ch.elexis.core.admin.InsufficientRightsException;
import ch.elexis.core.data.IActionDefinition;
import ch.elexis.easy.ui.dialogs.StandardDialogs;

/**
 * A Widget consisting of a TExtField and a Button
 * 
 * @author gerry
 * 
 */
public class InputButtonField extends Composite {
	FormToolkit tk = new FormToolkit(Display.getCurrent());
	Label lLabel;
	Text tText;
	Button bSend;
	
	/**
	 * Input field with a button
	 * 
	 * @param parent
	 *            parent composite
	 * @param label
	 *            label of the field
	 * @param buttonText
	 *            text on the button
	 * @param command
	 *            command to execute if the button is pressed.
	 */
	public InputButtonField(final Composite parent, final String label, final String buttonText,
		final IActionDefinition command){
		super(parent, SWT.NONE);
		setLayout(new FormLayout());
		lLabel = tk.createLabel(this, label);
		lLabel.setText(label);
		FormData fdLabel = new FormData();
		fdLabel.left = new FormAttachment(10, 5);
		fdLabel.top = new FormAttachment(0, 5);
		lLabel.setLayoutData(fdLabel);
		
		bSend = tk.createButton(this, buttonText, SWT.PUSH);
		FormData fdButton = new FormData();
		// fdButton.left=new FormAttachment(tText);
		fdButton.top = new FormAttachment(lLabel, 2);
		fdButton.right = new FormAttachment(100, -5);
		bSend.setLayoutData(fdButton);
		bSend.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e){
				try {
					command.execute(tText.getText());
				} catch (InsufficientRightsException e1) {
					e1.printStackTrace();
					StandardDialogs.showInsufficientRightsDialog(parent.getShell(), null);
				}
			}
			
		});
		
		tText = tk.createText(this, "", SWT.BORDER);
		FormData fdText = new FormData();
		fdText.left = new FormAttachment(10, 5);
		fdText.top = new FormAttachment(lLabel, 2);
		fdText.right = new FormAttachment(bSend, -10);
		tText.setLayoutData(fdText);
	}
}
